#include "BlockQueue.hpp"
#include "Task.hpp"
#include <pthread.h>
#include <ctime>
#include <unistd.h>
#include <sys/types.h>
using namespace std;

void* consumer(void* args)
{
    // BlockQueue<int> *bq = static_cast<BlockQueue<int>*>(args);
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task>*>(args);
    while(1)
    {
        sleep(1);
        Task t;
        //1.消费数据
        bq->Pop(&t);
        t();
        //int data = 0;
        //bq->Pop(&data);
        //2.进行处理
        //cout << "consumer data: "<< data << endl;
        cout << "consumer data: "<< t.PrintResult() << endl;
        
    }
    return nullptr;
}

void* productor(void* args)
{
    // BlockQueue<int> *bq = static_cast<BlockQueue<int>*>(args);// 生产者与消费者看到同一份公共资源
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task>*>(args);// 生产者与消费者看到同一份公共资源
    while(1)
    {
        //1.有数据
        //int data = rand()% 10 + 1; //[1,10]
        int data1 = rand()% 10; //[1,9]
        usleep(rand()%123);
        int data2 = rand()% 10; //[1,9]
        char oper = opers[rand() % opers.size()];
        Task t(data1,data2,oper);
        //2.进行生产
        //push
        //bq->Push(data);
        bq->Push(t);

        //cout << "productot data: " << data << endl;
        cout << "productot data: " << t.PrintTask() << endl;
        //sleep(1);
    }
    return nullptr;
}
int main()
{
    srand((uint16_t)time(nullptr) ^ getpid() ^ pthread_self());// 形成随机数
    // BlockQueue<int>* bq = new BlockQueue<int>();
    BlockQueue<Task>* bq = new BlockQueue<Task>();
    pthread_t c, p;// 消费者和生产者
    pthread_create(&c, nullptr, consumer, bq);
    pthread_create(&p, nullptr, productor, bq);

    pthread_join(c,nullptr);
    pthread_join(p,nullptr);
    return 0;
}